package com.fbms.module.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbms.common.constants.Constants;
import com.fbms.common.exception.ServiceException;
import com.fbms.common.result.Res;
import com.fbms.common.result.ResBuilder;
import com.fbms.module.system.bean.dto.SysUserDTO;
import com.fbms.module.system.bean.entity.SysUser;
import com.fbms.module.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 类描述：系统用户类
 *
 * @ClassName SysUserController
 * @Author HappyBoy
 * @Date 2023-03-15 15:15
 **/
@RestController
@RequestMapping("/users")
@Slf4j
public class SysUserController {

    /**
     * 构造方法注入
     * 系统用户实现类
     */
    private final ISysUserService sysUserService;

    @Autowired
    public SysUserController(ISysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    /**
     * 查询所有的用户信息
     * @return 用户信息列表
     */
    @GetMapping("/list")
    public Res<IPage<SysUser>> getUserList(@RequestParam(value = "userInfo", required = false) SysUser userInfo,
                                           @RequestParam(value = "size", defaultValue = "10", required = false) Integer size,
                                           @RequestParam(value = "offset", defaultValue = "0", required = false) Integer offset){
        IPage<SysUser> page = new Page<>(offset, size);
        return ResBuilder.of(sysUserService.getList(userInfo, page));
    }

    /**
     * 获取用户信息
     * @param userId 用户Id
     * @return 用户信息
     */
    @GetMapping("/{userId}")
    public Res<SysUserDTO> getUser(@PathVariable("userId") String userId){
        SysUser user = sysUserService.getById(userId);
        if(user == null){
            throw new ServiceException("用户不存在");
        }

        // TODO 获取角色信息

        // TODO 获取部门信息

        return null;
    }

    /**
     * 保存用户信息
     * @param sysUser 用户信息
     * @return true false
     */
    @PostMapping("")
    public Res<Integer> saveUser(@Valid @RequestBody SysUser sysUser){
        // 检查用户信息
        String userName = sysUser.getName();
        if (Constants.NOT_UNIQUE.equals(sysUserService.checkUserNameUnique(userName))){
            throw new ServiceException("用户名已存在");
        }
        String cellphone = sysUser.getCellphone();
        if(Constants.NOT_UNIQUE.equals(sysUserService.checkCellphoneUnique(cellphone))){
            throw new ServiceException("手机号已存在");
        }
        Integer num = sysUserService.saveUser(sysUser);
        return ResBuilder.of(num);
    }

    /**
     * 删除用户信息
     * @param userId 用户Id
     * @return 条数
     */
    @DeleteMapping("/{userId}")
    public Res<Integer> deleteUserById(@PathVariable("userId") String userId){
        // 将用户状态更新
//        int num = sysUserService.deleteUserById(userId);
        int num = 0;
        return ResBuilder.of(num);
    }

    /**
     * 更新用户信息
     * @param sysUser 用户信息
     * @return 条数
     */
    @PatchMapping("/{userId}")
    public Res<Integer> updateUser(@PathVariable("userId") String userId, @RequestBody SysUser sysUser){
        // TODO 校验用户名是否重复

        // TODO 校验邮箱是否重复

        // 更新信息
        int res = sysUserService.updateUser(userId, sysUser);
        return  ResBuilder.of(res);
    }







}
